package com.li.shop.controller;
import com.li.shop.domain.Admin;
import com.li.shop.domain.Merchants;
import com.li.shop.service.AdminService;
import com.li.shop.service.MerchantsService;
import com.li.shop.utils.JavaMailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/Admin")
public class AdminLogin {
    @Autowired
    private AdminService adminService;
    @Autowired
    private MerchantsService merchantsService;
    @RequestMapping(value = "/login" ,method= RequestMethod.POST)
    @ResponseBody
    public Map<String,String>UserLogin(HttpSession session, HttpServletRequest request,HttpServletResponse response)  {

        Map<String,String>map=new HashMap<>();
        String email=request.getParameter("email");
        String password=request.getParameter("password");
        Admin admin = adminService.login(email, password);
        Merchants merchants=merchantsService.login(email,password);
        if (admin != null&& merchants==null) {
            session.setAttribute("permission",admin.getPermission());
//            return "redirect:/Admin/judge";
            map.put("result","ok");
        }
         if(merchants!=null&& admin==null)
         {
             session.setAttribute("permission",merchants.getPermission());

//             return "redirect:/Admin/judge";
             map.put("result","ok");
         }
         if(admin==null&&merchants==null) {
//               return "back/login";
             map.put("result","false");
         }

        return map;
    }
    @RequestMapping("judge")
    public String judge(HttpSession session, Model model)
    {
        Integer permission=(int)session.getAttribute("permission");
        model.addAttribute("permission",permission);
        return "back/shopmanage";
    }
    @RequestMapping(value = "zhuce",method = RequestMethod.POST)
    public String zhuce(Admin admin, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String email=request.getParameter("email");
        String code=request.getParameter("code");
        session=request.getSession(false);
        if(session!=null&&session.getAttribute("email")!=null&session.getAttribute("code")!=null)
        {
            String sessionEmail=(String)session.getAttribute("email");
            String sessionCode=(String)session.getAttribute("code");
            if (sessionEmail.equals(email)&&sessionCode.equals(code))
            {
                adminService.add(admin);
                return "back/login";
            }
        }
        return "back/register";
    }
    @RequestMapping(value="registerAuthCode",method = RequestMethod.POST)
    public void registerAuthCode(Admin admin, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String email=request.getParameter("email");
        Random rm=new Random();
        String code="";
        for(int i=0;i<6;i++)
        {
            int num=rm.nextInt(26)+65;
            code +=(char)num;
        }
        int res= JavaMailUtils.sendEmail(new String[]{
                email //这里就是一系列的收件人的邮箱了
        }, "商城邮箱验证", "欢迎注册商城后台，您的验证码是 <br />"+code);
        session=request.getSession(true);
        session.setAttribute("code",code);
        session.setAttribute("email",email);
        session.setMaxInactiveInterval(60);

        //adminService.add(admin);
    }
}